package com.guigu.buy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.buy.dto.PurOrder_Supplier;
import com.guigu.buy.dto.Storage_Detail;
import com.guigu.buy.dto.Storage_Stock;
import com.guigu.buy.pojo.PurchaseOrder;
import com.guigu.buy.pojo.Storage;
import com.guigu.buy.mapper.StorageMapper;
import com.guigu.buy.pojo.StorageDetails;
import com.guigu.buy.service.IPurchaseOrderService;
import com.guigu.buy.service.IStorageDetailsService;
import com.guigu.buy.service.IStorageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.buy.vo.StorageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements IStorageService {
    @Autowired
    private StorageMapper storageMapper;
    @Autowired
        private IStorageDetailsService storageDetailsService;
    @Autowired
    private IPurchaseOrderService purchaseOrderService;
    @Override
    public String addStorage(Storage_Detail storage_detail,int id) {
        int total=0;
        for (StorageDetails s:storage_detail.getList()){
            System.out.println(s);
            total+=s.getSubtotal();
        }
        storage_detail.setStorageTotal(total);
        storage_detail.setId(null);
        boolean save = this.save(storage_detail);
        if(save){
            storageDetailsService.add(storage_detail.getList());
            PurchaseOrder purchaseOrder=new PurchaseOrder();
            purchaseOrder.setId(id);
            purchaseOrder.setConveyTag("P001-3");
            purchaseOrderService.updateById(purchaseOrder);
            return "ok";
        }
        return "no";
    }

    @Override
    public String getMaxDesignId() {
        Storage storage=storageMapper.queryMax();
        if(storage==null){
            return null;
        }
        String designId=storage.getStorageId();
        return designId;
    }

    @Override
    public PageInfo<Storage_Stock> queryAll(int pageNo, int pageSize, StorageVo vo) {
        List<Storage_Stock> list=storageMapper.queryAll(vo);
        PageHelper.startPage(pageSize,pageNo );
        PageInfo<Storage_Stock> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
}
